package com.tom.service.impl;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tom.util.HttpRequest;
import com.tom.util.JDBCUtil;

public class TuDouJinFu {
	/**
	 * 
	 * @author hanpengda
	 * @date 2018年5月3日
	 * @TODO 获取土豆金服的url地址
	 */
	public static void work(){
		String url = "https://www.tudoujf.com/loan/loan/listtender";
		ArrayList<String> list = new ArrayList<String>();
		for (int i = 1; i <= 150; i++) {
			String param = "page=";
			param += i;
			String result = HttpRequest.sentPost(url, param);
		//	System.out.println(result);
			JSONObject jsonObject = JSON.parseObject(result);
			//System.out.println(jsonObject);
			JSONArray jArray = jsonObject.getJSONArray("items");
			//System.out.println(jArray);
			for(int j = 0; j < jArray.size(); j++){
				JSONObject jObject = jArray.getJSONObject(j);
			//	System.out.println(jObject);
				String status_name = jObject.getString("status_name");
				if (status_name.equals("还款中")) {
					String Url = jObject.getString("id");
					System.out.println("第"+i+"页"+"第"+j+"个:"+Url);
					list.add(Url);
				}	
			}
		}
		/*try {
			JDBCUtil.insertUrls(list,"qss_tudoujinfu_url");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		
	}
	/**
	 * 
	 * @author hanpengda
	 * @date 2018年5月3日
	 * @TODO 获取土豆金服业务json数据
	 */
	public static void work2(){
		String url [] = {"https://www.tudoujf.com/common/loan/loaninfo","https://www.tudoujf.com/common/experience/experienceList"};
		ArrayList<String> list = new ArrayList<String>();
		ArrayList<String>  dArrayList = new ArrayList<String>();
		String param = "";
		int page;
		try {
			list = JDBCUtil.getUrls("qss_tudoujinfu_url");
			for(int i = 0; i < list.size(); i++){
				String id = list.get(i);
				//String id = "1588";
				param = "id="+id;
				String result = HttpRequest.sentPost(url[0], param);
				JSONObject jsonObject = JSON.parseObject(result);
				//System.out.println(jsonObject);
				JSONObject data = jsonObject.getJSONObject("data");
//----------------------------------------------------------------------------------------
				param = "loanId="+id;
				String result1 = HttpRequest.sentPost(url[1], param);
				JSONObject jsonObject1 = JSON.parseObject(result1);
				Integer totalPage = jsonObject1.getInteger("total_pages");
				JSONArray array = new JSONArray();
				for (int j = 1; j <= totalPage; j++) {
					page = j;
					param = "loanId="+id+"&page="+page;
					result = HttpRequest.sentPost(url[1], param);
					JSONObject object = JSON.parseObject(result);
					JSONArray jsonArray = object.getJSONArray("items");
					for (int k = 0; k < jsonArray.size(); k++) {
						array.add(jsonArray.get(k));
					}
				}
				//System.out.println(array);
				//System.out.println(data);
				Date now = new Date(); 
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
				String time = dateFormat.format(now);
				String sql = "insert into qss_tudoujinfu_itemdata(id,json,json1,insertdate) values("+id+","+"'"+data+"'"+","+"'"+array+"'"+","+"'"+time+"'"+")";
				dArrayList.add(sql);
				System.out.println(sql);
				System.out.println("---------------------"+i+i+i+i+i+i+i+i+i+i+i+i+"--------------------");
			}
			//JDBCUtil.insertData(dArrayList);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

	/**
	 * 
	 * @author hanpengda
	 * @date 2018年5月4日
	 * @TODO 处理土豆金服业务json数据
	 */
	public static void work3(){
		try {
			ArrayList<HashMap<String, String>> list = JDBCUtil.getJsonData("json","qss_tudoujinfu_itemdata");
			ArrayList<HashMap<String, String>> list1 = JDBCUtil.getJsonData("json1","qss_tudoujinfu_itemdata");
			ArrayList<String> result = new ArrayList<String>();
			for (int i = 0; i < list.size(); i++) {
				HashMap<String, String> map = list.get(i);
				HashMap<String, String> map1 = list1.get(i);
				//System.out.println(map1);
				//System.out.println(map);
				String json = map.get("json");
				//System.out.println(json);
				String regEx_html = "<[^>]+>";
				Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);  
			    Matcher m_html = p_html.matcher(json);  
			    json = m_html.replaceAll("");
			    json = json.replaceAll("&nbsp;", "");
				JSONObject loanInfo = JSONObject.parseObject(json);
				//System.out.println(loanInfo);
				JSONObject loan_info = JSONObject.parseObject(loanInfo.getString("loan_info"));
				//System.out.println(loan_info);
				JSONObject repayType = JSONObject.parseObject(loanInfo.getString("repay_type"));
				//System.out.println(repayType);
				String name = loan_info.getString("name");
				String amount = loan_info.getString("amount");
				String apr = loan_info.getString("apr");
				String period_name = loan_info.getString("period_name");
				String progress = loan_info.getString("progress");
				String repay_type = repayType.getString("name");
				String status_name = loan_info.getString("status_name");
				String contrall_contents = loan_info.getString("contrall_contents");
				String promise_contents = loan_info.getString("promise_contents");
				String project_contents = loan_info.getString("project_contents");
				Date now = new Date(); 
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
				String time = dateFormat.format(now);
				String json1 = map1.get("json1");
				JSONArray jArray = JSONArray.parseArray(json1);
				String jyjl = "";
				for (int j = 0; j < jArray.size(); j++) {
					JSONObject object = jArray.getJSONObject(j);
					String jyjlAmount = object.getString("amount");
					String member_name = object.getString("member_name");
					String add_time = object.getString("add_time");
					String invest_source = object.getString("invest_source");
					jyjl += jyjlAmount+"##"+member_name+"##"+add_time+"##"+invest_source+"==";
					System.out.println(jyjl);
				}
				jyjl = jyjl.substring(0, jyjl.length()-2);
				System.out.println(jyjl);
				String sql = "insert into qss_tudoujinfu_bussiness_data(name,amount,apr,period_name,progress,repay_type,status_name,contrall_contents,promise_contents,project_contents,tenderinfo,insertdate) "
						+ "values("+"'"+name+"'"+","+"'"+amount+"'"+","+"'"+apr+"'"+","+"'"+period_name+"'"+","+"'"+progress+"'"+","+"'"+repay_type+"'"+","+"'"+status_name+"'"+","+"'"+contrall_contents+"'"+","+"'"+promise_contents+"'"+","+"'"+project_contents+"'"+","+"'"+jyjl+"'"+","+"'"+time+"'"+")";
				System.out.println("第"+i+"条"+sql);
				result.add(sql);
				//System.out.println(name+" "+amount+" "+apr+" "+period_name+" "+progress+" "+repay_type+" "+status_name+" "+contrall_contents+" "+promise_contents+" "+project_contents);
			}
			JDBCUtil.insertData(result);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 
	 * @author hanpengda
	 * @date 2018年5月7日
	 * @TODO 土豆金服运营数据--成交数据
	 */
	public static void work4(){
		String param = "dealmap";
		String [] columns = {"date","totalamt"};
		String insertTableName = "qss_tudoujinfu_operation_dealmap";
		JDBCUtil.tdjfInsertData(param, columns, insertTableName);
	}
	
	/**
	 * 
	 * @author hanpengda
	 * @date 2018年5月7日
	 * @TODO 土豆金服运营数据--注册人年龄分布
	 */
	public static void work5(){
		String param = "ageRangeMap";
		String [] columns = {"age","pnumber"};
		String insertTableName = "qss_tudoujinfu_operation_ageRangeMap";
		JDBCUtil.tdjfInsertData(param, columns, insertTableName);
	}
	
	/**
	 * 
	 * @author hanpengda
	 * @date 2018年5月7日
	 * @TODO 土豆金服运营数据--注册用户投资占比
	 */
	public static void work6(){
		String param = "tenderRangeMap";
		String [] columns = {"type","ratio"};
		String insertTableName = "qss_tudoujinfu_operation_tenderRangeMap";
		JDBCUtil.tdjfInsertData(param, columns, insertTableName);
	}
	
	/**
	 * 
	 * @author hanpengda
	 * @date 2018年5月7日
	 * @TODO 土豆金服运营数据--投资金额分布
	 */
	public static void work7(){
		String param = "amountRangeMap";
		String [] columns = {"investamt","pnumber"};
		String insertTableName = "qss_tudoujinfu_operation_amountRangeMap";
		JDBCUtil.tdjfInsertData(param, columns, insertTableName);
	}
	
	/**
	 * 
	 * @author hanpengda
	 * @date 2018年5月7日
	 * @TODO 土豆金服运营数据--融资期限分布
	 */
	public static void work8(){
		String param = "loanRangeMap";
		String [] columns = {"financedate","pnumber"};
		String insertTableName = "qss_tudoujinfu_operation_loanRangeMap";
		JDBCUtil.tdjfInsertData(param, columns, insertTableName);
	}
	
	/**
	 * 
	 * @author hanpengda
	 * @date 2018年5月7日
	 * @TODO 土豆金服运营数据--地区人数分布
	 */
	public static void work9(){
		String param = "areaUserMap";
		String [] columns = {"area","pnumber"};
		String insertTableName = "qss_tudoujinfu_operation_areaUserMap";
		JDBCUtil.tdjfInsertData(param, columns, insertTableName);
	}
	
	/**
	 * 
	 * @author hanpengda
	 * @date 2018年5月7日
	 * @TODO 土豆金服运营数据--地区投资金额分布
	 */
	public static void work10(){
		String param = "areaTenderMap";
		String [] columns = {"area","investamt"};
		String insertTableName = "qss_tudoujinfu_operation_areaTenderMap";
		JDBCUtil.tdjfInsertData(param, columns, insertTableName);
	}
	
	/**
	 * 
	 * @author hanpengda
	 * @date 2018年5月7日
	 * @TODO 土豆金服运营数据--地区融资金额分布
	 */
	public static void work11(){
		String param = "areaLoanMap";
		String [] columns = {"area","financeamt"};
		String insertTableName = "qss_tudoujinfu_operation_areaLoanMap";
		JDBCUtil.tdjfInsertData(param, columns, insertTableName);
	}
}
